package com.demo.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter;
import org.springframework.security.oauth2.provider.token.store.JwtTokenStore;
import org.springframework.security.oauth2.provider.token.store.KeyStoreKeyFactory;

@EnableAuthorizationServer
@Configuration
public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Autowired
    private AuthenticationManager authenticationManager;

    @Qualifier("userServiceDetailsServiceImpl")
    @Autowired
    private UserDetailsService userDetailsService;

//    @Autowired
//    private ClientDetailsService clientDetailsService;


    /**
     * 添加第三方的客户端
     */
    @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
        clients.inMemory()
                .withClient("coin-api") // 第三方客户端的名称
                .secret(passwordEncoder.encode("coin-secret")) //  第三方客户端的密钥
                .scopes("all") //第三方客户端的授权范围
                .authorizedGrantTypes("password","refresh_token")
                .accessTokenValiditySeconds(7 * 24 *3600) // token的有效期
                .refreshTokenValiditySeconds(30 * 24 * 3600)// refresh_token的有效期
                .and()
                .withClient("inside-app")
                .secret(passwordEncoder.encode("inside-secret"))
                .authorizedGrantTypes("client_credentials")
                .scopes("all")
                .accessTokenValiditySeconds(Integer.MAX_VALUE) ;

               ;
			  


//    @Bean
//    @Order(Ordered.HIGHEST_PRECEDENCE)
//    public DefaultTokenServices defaultTokenServices() {
//        DefaultTokenServices defaultTokenServices = new DefaultTokenServices();
//        defaultTokenServices.setClientDetailsService(clientDetailsService);
//        defaultTokenServices.setTokenStore(jwtTokenStore());
//        defaultTokenServices.setTokenEnhancer(jwtAccessTokenConverter());
//        defaultTokenServices.setAccessTokenValiditySeconds(7200);
//        defaultTokenServices.setRefreshTokenValiditySeconds(259200);
//        defaultTokenServices.setSupportRefreshToken(true);
//        return defaultTokenServices;
		super.configure(clients);
    }

    /**
     * 配置验证管理器
     */

    @Override
    public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
        endpoints.authenticationManager(authenticationManager)
                .userDetailsService(userDetailsService)
                .tokenStore(jwtTokenStore())
                .tokenEnhancer(jwtAccessTokenConverter());//将token设置成json格式

        super.configure(endpoints);
    }


    /**
     * 根据连接生成redisToken对象
     * @return
     */
//    public TokenStore redisTokenStore(){
//        return new RedisTokenStore(redisConnectionFactory);
//    }

    /**
     * 生成JWTtoken对象
     */
        private TokenStore jwtTokenStore(){
            JwtTokenStore jwtTokenStore = new JwtTokenStore(jwtAccessTokenConverter());
            return jwtTokenStore;
    }

    public JwtAccessTokenConverter jwtAccessTokenConverter(){
            JwtAccessTokenConverter tokenConverter = new JwtAccessTokenConverter();
            ClassPathResource classPathResource = new ClassPathResource("coinexchange.jks");
            KeyStoreKeyFactory keyStoreKeyFactory =new KeyStoreKeyFactory(classPathResource,"coinexchange".toCharArray());
            tokenConverter.setKeyPair(keyStoreKeyFactory.getKeyPair("coinexchange","coinexchange".toCharArray()));
            return tokenConverter;
    }
}
